<?php
/**
 * Created by XM·Tan.
 * Motto: Who shuts love out, in turn shall be shut out from love.
 * Date: 2021/3/23 18:10
 */

namespace app\admin\business;


use app\common\business\BusBase;
use app\common\model\mysql\AdminRole as AdminRoleModel;
use app\common\lib\ArrayLib;
use think\facade\Log;

class AdminRole extends BusBase {

    public $model = null;

    public function __construct() {
        $this->model = new AdminRoleModel();
    }

    /**
     * 获取角色列表数据
     * @param $data
     * @param int $num
     * @return array
     */
    public function getLists($data, $num = 10) {

        $likeKeys = []; // 如果没有定义这个 有时候会报错
        if (!empty($data)) {
            $likeKeys = array_keys($data);
        }
        try {
            $list = $this->model->getLists($likeKeys, $data, $num);
            $result = $list->toArray();
        } catch (\Exception $e) {
            Log::error('common-business-AdminRole-getLists: ' . $e->getMessage());
            $result = ArrayLib::getPaginateDefaultData($num);
        }
        return $result;
    }

    /**
     * 管理员下拉列表获取数据
     * @return array|bool
     */
    public function getRoleLists() {
        try {
            $list = $this->model->getNormalListsAll();
            $result = $list->toArray();
        } catch (\Exception $e) {
            Log::error('common-business-AdminRole-getRoleLists: ' . $e->getMessage());
            return false;
        }
        return $result;
    }

    /**
     * 新增角色
     * @param $data
     * @return bool|mixed
     */
    public function insertData($data) {
        try {
            $id = $this->add($data);
            if (!$id) {
                return $id;
            }
            return true;
        } catch (\Exception $e) {
            Log::error('common-business-AdminRole-insertData: ' . $e->getMessage());
            return false;
        }
    }

    /**
     * 更新角色
     * @param $data
     * @return bool|mixed
     */
    public function updateData($data) {

        try {
            $id = $this->edit($data['id'],$data);
            if (!$id) {
                return $id;
            }
            return true;
        } catch (\Exception $e) {
            Log::error('common-business-AdminRole-updateData: ' . $e->getMessage());
            return false;
        }
    }

}